define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'devices/index',
                    add_url: 'devices/add',
                    edit_url: 'devices/edit',
                    del_url: 'devices/del',
                    table: 'sstalink_device'
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'deviceid',
                sortName: 'deviceid',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title:__('Name')},
                        {field: 'deviceid', title: __('Deviceid')},
                        {field: 'secret', title:__('Secret')},
                        {field: 'state', title:__('State'),formatter: function(value,row,index){
                            switch (value){
                                case -1:    return "已锁定";
                                case 0:     return "离线";
                                case 1:     return "在线";
                            }
                        }},
                        {field: 'last_login_time',title:__('LastLoginTime')},
                        {field: 'operate', title: __('Operate'), table: table, buttons:[
                            {name: 'sensor_manage', text: '传感器管理', title: '传感器管理', classname: 'btn btn-xs btn-info btn-addtabs', url: 'sensor/index'}
                            ],events: Table.api.events.operate,formatter:Table.api.formatter.operate},
                        {field: 'state',title:"锁", formatter:function(value,row,index){
                            var type="";
                            switch (value){
                                case -1:   type="解锁";break;
                                case 0:    type="锁定";break;
                                case 1:    type="锁定";break;
                            }
                            return "<a href='devices/unlock?ids="+row.deviceid+"' class='fa fa-lock btn btn-xs btn-danger btn-primary'> "+type+"</a>";
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }

        }
    };
    return Controller;
});